#include "PixelObjectMap.h"

#include "Render/PixelObject/RenderPixelObjectMap.h"
#include "Render/PixelObject/PixelObjectMapID.h"

RPixelObjectMap::RPixelObjectMap()
{
    RenderData=std::make_shared<CRenderPixelObjectMap>();
    RenderData->ID=WH::MakeThreadSafeSharedPtr<CPixelObjectMapID>();
    RenderData->ID->Map=this;
}

void RPixelObjectMap::WriteData(
    uint32_t InViewportWidth
    , uint32_t InViewportHeight
    , uint32_t InWidth
    , uint32_t InHeight
    , uint32_t InPitch
    , TVector<uint8_t> &&InData)
{
    ViewportWidth = InViewportWidth;
    ViewportHeight = InViewportHeight;
    Width = InWidth;
    Height = InHeight;
    Pitch = InPitch;

    Data = std::move(InData);
}

std::shared_ptr<CPixelObject> RPixelObjectMap::QueryPixelObject(const SIntPoint &InViewportPosition)
{
    SIntPoint PositionInMap;
    PositionInMap.X = ((float)InViewportPosition.X / ViewportWidth) * Width ;
    PositionInMap.Y = ((float)InViewportPosition.Y / ViewportHeight) * Height ;

    if (InViewportPosition.X < 0 
    || InViewportPosition.X >= ViewportWidth 
    || InViewportPosition.Y < 0 
    || InViewportPosition.Y >= ViewportHeight)
    {
        //像素位置超出范围
        return nullptr;
    }

    auto IDPtr=(SPixelObjectID*)(
        Data.Data()
        +PositionInMap.Y*Pitch
        +PositionInMap.X*sizeof(SPixelObjectID)
    );

    return PixelObjects.GetObject(*IDPtr);
}
